package crmdna.api.endpoint;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.client.Client;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;
import crmdna.teacher.Teacher;
import crmdna.teacher.Teacher.TeacherProp;

@Api(name = "teacher")
public class TeacherApi {
	@ApiMethod(path = "create", httpMethod = HttpMethod.POST)
	public APIResponse create(@Named("client") String client,
			@Named("email") String email, @Named("group_id") long groupId,
			User user) {

		try {
			TeacherProp prop = Teacher.create(client, email, groupId,
					Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "getAll", httpMethod = HttpMethod.GET)
	public APIResponse getAll(@Named("client") String client) {

		try {
			Client.ensureValid(client);
			List<TeacherProp> props = Teacher.getAll(client);

			return new APIResponse(Status.SUCCESS, props);
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "rename", httpMethod = HttpMethod.GET)
	public APIResponse update(@Named("client") String client,
			@Named("teacher_id") long teacherId,
			@Nullable @Named("new_email") String newEmail,
			@Nullable @Named("new_group_id") Long newGroupId, User user) {

		try {
			TeacherProp prop = Teacher.update(client, teacherId, newEmail,
					newGroupId, Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "delete", httpMethod = HttpMethod.GET)
	public APIResponse delete(@Named("client") String client,
			@Named("teacher_id") long teacherId, User user) {

		try {
			Client.ensureValid(client);
			Teacher.delete(client, teacherId, Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, "Venue [" + teacherId
					+ "] deleted");
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
}
